package com.jason.service;

import com.jason.mapper.UserMapper;
import com.jason.model.entity.User;
import com.jason.utlis.CommonUtils;
import com.jason.utlis.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    /**
     * 注册逻辑，前端传过去userInfo，判断手机号，名字，密码是否包含
     * @param userInfo
     * @return
     */
    @Override
    public int save(Map<String, String> userInfo) {
       User user= parseToUser(userInfo);
       if (user!=null){
          return userMapper.save(user);
       }else {
           return -1;
       }
    }
    /**
     * 解析user对象
     * @param userInfo
     * @return
     */
    private User parseToUser(Map<String, String> userInfo) {
        if (userInfo.containsKey("phone")&&userInfo.containsKey("pwd")&&
        userInfo.containsKey("name")){
            User user = new User();
            user.setName(userInfo.get("name"));
            user.setHeadImg(getRandomImg());
            user.setCreateTime(new Date());
            user.setPhone(userInfo.get("phone"));
            String pwd = userInfo.get("pwd");
            //MD5加密
            user.setPwd(CommonUtils.MD5(pwd));
            return user;
        }else {
            return null;
        }
    }

    /**
     * 放在CDN上的随机头像
     */
    private static final String [] headImg = {
            "https://xd-video-pc-img.oss-cnbeijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
            "https://xd-video-pc-img.oss-cnbeijing.aliyuncs.com/xdclass_pro/default/head_img/11.jpeg",
            "https://xd-video-pc-img.oss-cnbeijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
            "https://xd-video-pc-img.oss-cnbeijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
            "https://xd-video-pc-img.oss-cnbeijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg"
    };

    private String getRandomImg(){
        int size=headImg.length;
        Random random = new Random();
        int index=random.nextInt(size);
        return headImg[index];
    }

    @Override
    public String findByPhoneAndPwd(String phone, String pwd) {
       User user= userMapper.findByPhoneAndPwd(phone,CommonUtils.MD5(pwd));
        if (user==null){
            return null;
        }else {
            String token = JWTUtils.geneJsonWebToken(user);
            return token;
        }
    }

    @Override
    public User findByUserId(Integer userId) {
       User user= userMapper.findByUserId(userId);
       user.setPwd("");
        return user;
    }
//    @Override
//    public User findByPhone(String phone) {
//        return null;
//    }
}
